 /**
 * Created by seeker910 on 2017/3/28.
 * 富文本内容编辑页面，应该放在redj/app目录中
 */
Rsd.define('Rsd.tool.RichTextEditor', {
    extend: 'Rsd.container.Dialog',
    requires: ['Rsd.control.RichTextBox','Rsd.form.File'],
    title: '富文本编辑器',
    layout: {type:'vbox',align:'center'},
    /**
     * 文档内容格式:text/html,text/plain,application/json,text/css,text/md,仅html可以使用富文本编辑菜单
     */
    contentType:'json',
    /**
     * 
     */
    services:{
        /**
         * 内容下载地址
         */
        downloadUrl:'',
        /**
         * 内容上传地址
         */
        uploadUrl:'',
    },
    /**
     * 
     */
    saveHandler:'',
    /**
     * 
     */
    autoLoad:true,
    /**
     * 
     */
    items: [ 
        {
            xtype:'rich-text-box',  
            readonly:true,
            contentType:'doc',
            margin:'0 0 20px 0',
            flex:1,
            width:'96%',  
        } 
    ],
    /*
     * */
    constructor: function RichTextEditor(config) {

        config = config || {};
        this.apply(config);
        this.items[0].contentType = this.contentType;
        //this.items[0].readonly = this.readonly;
        this.items[0].label = {
            xtype:'container',  
            position:'top',
            height:40,
            header:{content:'数据类型：' + this.contentType ,position:'left',align:'left',style:{backgroundColor:'transparent'}},
            layout:{type:'hbox',align:'right'},
            items:[
                {
                    xtype:'file',  
                    width:500,
                    label:'从本地获取',
                    align:'right',
                    style:{element:'ctrl',border:'none',borderBottom:'1px grey solid'}, 
                    hidden:this.readonly,
                    onchange:'on_file_selected'
                },
                {
                    xtype:'link',  
                    width:60,
                    label:{content:'✒️',position:'left',space:0},  
                    style:{fontSize:'100%',textAlign:'center'},
                    text:'编辑',
                    hidden:true,
                    handler:'btn_edit'
                },
                {
                    xtype:'link',  
                    width:60,
                    label:{content:'💾',position:'left',space:0}, 
                    style:{fontSize:'100%',textAlign:'center'},
                    text:'保存',
                    hidden:this.readonly,
                    handler:'btn_save'
                }
            ]
            
        }; 
 
    },
    /**
     * 
     */
    load:function load(data)
    {
        //console.trace('data is ',data);
        var me = this; 
        me.items[0].setReadOnly(this.readonly);  

        if(!me.services || Rsd.isEmpty(me.services.downloadUrl))
        {
            me.items[0].setValue(data || '');
            return;
        }

        Rsd.app.requestService(me.services.downloadUrl,{
                    modelType:me.modelType,
                    name:me.fieldName,
                    key:me.key
                },
                function(resp){
                    //console.log(resp.data);
                    var _value = resp.data||'';
                    if(me.contentType.toLowerCase() == 'application/json')
                    { 
                            var success = true;
                            var msg = "";
                            try
                            {
                                _value = JSON.parse(_value);
                                _value = JSON.stringify(_value,null,2);
                            }
                            catch(err)
                            {
                                success = false;
                                msg = err.message;
                            }
                            if(success==false)
                            {
                                alert(msg); 
                            }
                    } 
                    me.items[0].setValue(_value);
        }); 
    },
    /*
    * */
    btn_edit:function btn_edit( ) {
        this.items[0].setReadOnly(false);
        //console.log(this.items[0].label);
        this.items[0].label.content.items[0].hide();
        this.items[0].label.content.items[1].show(); 
    },
    /*
    * */
    btn_save:function btn_save() {
        var me = this;
        this.items[0].setReadOnly(true); 
        //this.items[0].label.content.items[0].show();
        //this.items[0].label.content.items[1].hide(); 
        var text = this.items[0].getValue();
        //console.log(text);
        //debugger;
        if(me.contentType.toLowerCase() == 'application/json')
        {
            var success = true;
            var msg = "";
            try
            {
                text = JSON.parse(text);
            }
            catch(err)
            {
                success = false;
                msg = err.message;
            }
            if(success==false)
            {
                alert(msg);
                return;
            }
        }
        if(!me.services || Rsd.isEmpty(me.services.uploadUrl))
        {
            //console.log(text);
            if(this.saveHandler)
            {
                this.funApplyByIOC(this.saveHandler,[text]);
            }
            return;
        }

        var file = Rsd.stringToFile(JSON.stringify(text),me.contentType,  me.Key + me.fieldName + '.txt');
        var data = { 
            modelType:me.modelType,
            name:me.fieldName,
            key:me.key
        }; 
        data['file'] = file;
        data['desc'] = me.desc||'这是来自富文本编辑器上传的内容';
        //console.log(data);
        top.Rsd.postFile(me.services.uploadUrl,data,function(resp){
            //console.log('post file :' ,resp);
            Rsd.popup('保存成功'); 
        });
    },
    /**
     * 
     * @param {*} s 
     * @param {*} e 
     * @returns 
     */
    on_file_selected:function on_file_selected(s,e)
    {
        //console.log('this is file',s,e);
        var me = this;
        var file = e.target.files[0];
        var reader = new FileReader();
        reader.onload = (event) => {
          var fileContent = event.target.result;
          // 在这里可以对文件内容进行处理 
          me.items[0].setValue(fileContent);
        };
        reader.readAsText(file);
        return true;
    } 
});